package operateBusiness;

import models.MyConfig;
import models.ReturnJson;
import models.TestServerBusinessImpl;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * Created by yangxianming on 2023/12/4.
 */
public class DeleteFile implements TestServerBusinessImpl {
    // 如果访问到文件，会将末尾加上.delete。换句话说就是逻辑删除
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        String filePath = request.getParameter("filePath");
        String rowIndex = request.getParameter("rowIndex");

        System.out.println("filePath:" + filePath);


        //判断excelPath是目录还是文件
        File file = new File(filePath);
        if (file.isDirectory()){
            System.out.println("目标是目录");
        }

        if (file.isFile()){
            return delete_file(filePath).toString();
        }

        ReturnJson returnJson = new ReturnJson();
        returnJson.setApiStatus(0);
        returnJson.setResult("can not found excelPath:" + filePath);
        return returnJson.toString();
    }

    private ReturnJson delete_file(String filePath){
        ReturnJson returnJson = new ReturnJson();
        try {
            String current_time = String.valueOf(System.currentTimeMillis());
            File file = new File(filePath);
            File new_file = new File(filePath + "delete." + current_time);
            Boolean is_delete = file.renameTo(new_file);
            returnJson.setApiStatus(1);
            returnJson.setResult(is_delete);
        }catch (Exception e){
//            e.printStackTrace();
            returnJson.setApiStatus(0);
            returnJson.setResult(e.getMessage());
        }
        return returnJson;
    }
}
